package com.kanas.webak.config.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.kanas.framework.business.BusinessHandler;
import com.kanas.framework.business.ResponseMessage;
import com.kanas.framework.support.ControllerSupport;
import com.kanas.webak.config.domain.Sequence;
import com.kanas.webak.config.service.SequenceService;

@Controller
@RequestMapping("app/config/sequence")
public class SequenceController extends ControllerSupport {

	private SequenceService sequenceService;

	@RequestMapping(value="", method=RequestMethod.GET)
	public ModelAndView properties(){
		ModelAndView mv=new ModelAndView();
		mv.addObject("sequence", sequenceService.findAll());
		mv.setViewName("modules/sysadmin/sequence");
		return mv;
	}
	
	@RequestMapping(value="/all.json",method=RequestMethod.GET)
	public @ResponseBody Map<String,Object> listAll(){
		Map<String,Object> response=new HashMap<String,Object>();
		response.put("identifier", "id");
		response.put("label", "code");
		response.put("items", this.getSequenceService().findAll());
		return response;
	}
	
	@RequestMapping(value="/new.html", method=RequestMethod.GET)
	public ModelAndView addNew(){
		ModelAndView mv=new ModelAndView();
		mv.setViewName("modules/sysadmin/sequence_new");
		return mv;
	}
	
	@RequestMapping(value="new.json", method=RequestMethod.POST)
	public @ResponseBody ResponseMessage addNew(final Sequence seq) {
		ResponseMessage response = ResponseMessage.handleException(new BusinessHandler(){
			@Override
			public Object handle() throws Exception {
				return sequenceService.addNew(seq);
			}
		}, logger);
		return response;
	}
	
	@RequestMapping(value="/update/{sid}.html", method=RequestMethod.GET)
	public ModelAndView groupEditView(@PathVariable("sid")String sid){
		ModelAndView mv=new ModelAndView();
		mv.setViewName("modules/sysadmin/sequence_edit");
		mv.addObject("seq", sequenceService.findOne(sid));
		return mv;
	}
	
	@RequestMapping(value="/update.json")
	public @ResponseBody ResponseMessage update(final Sequence seq){
		ResponseMessage response = ResponseMessage.handleException(new BusinessHandler(){
			@Override
			public Object handle() throws Exception {
				return sequenceService.update(seq);
			}
		}, logger);
		return response;
	}
	
	@RequestMapping(value="/remove/{sid}.json")
	public @ResponseBody ResponseMessage remove(@PathVariable("sid") final String id){
		ResponseMessage response = ResponseMessage.handleException(new BusinessHandler(){
			@Override
			public Object handle() throws Exception {
				sequenceService.remove(id);
				return null;
			}
		}, logger);
		return response;
	}
	
	public SequenceService getSequenceService() {
		return sequenceService;
	}

	@Resource(name="com.kanas.webak.config.service.sequenceservice")
	public void setSequenceService(SequenceService sequenceService) {
		this.sequenceService = sequenceService;
	}
}
